package com.site.dao.impl.backend.system;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.site.dao.backend.system.MenuDao;
import com.site.dto.backend.system.ResourseBizBean;
import com.site.model.backend.system.ResourseInfo;
import com.site.util.PropertyUtils;
import com.site.util.StringUtils;
import com.site.util.base.dao.BaseDaoImpl;
import com.site.util.base.dao.HQLHelper;
import com.site.util.extremetable.QueryPage;

/**
 * 
 * @author Lee 2012-12-24
 *
 */
@Repository
public class MenuDaoImpl extends BaseDaoImpl implements MenuDao {

	@Override
	public List<ResourseInfo> getMenuList(QueryPage queryPage, ResourseBizBean vo) {
		HQLHelper hqlHelper = new HQLHelper("ResourseInfo");
		if(null != vo){
			if(!StringUtils.isEmpty(vo.getName()) && vo.getName().equals("root")){
				hqlHelper.addIsNull("parentResourseInfo.id");
			} else {
				hqlHelper.addEqual("parentResourseInfo.id", vo.getName());
			}
			if(null != vo.getParentResourseBizBean() && null != vo.getParentResourseBizBean().getId()){
				hqlHelper.addEqual("parentResourseInfo.id", vo.getParentResourseBizBean().getId());
			} 
			if(null != vo.getId()){
				hqlHelper.addEqual("id", vo.getId());
			}
		}
		hqlHelper.addEqual("markDelete", "N");
		if(null != queryPage){
			return super.findByHQL(queryPage, hqlHelper.getHQL(),null,false);
		} else {
			return super.findByHQL(hqlHelper.getHQL(),null);
		}
	}

	
	
	@Override
	public void update(ResourseBizBean obj) {
		if(null != obj){
			ResourseInfo po = super.getEntityById(ResourseInfo.class, obj.getId());
			if(null != po){
				PropertyUtils.copyNotNullProperties(po, obj);
			}
			super.update(po);
		}		
	}

	@Override
	public void delete(ResourseBizBean obj) {
		if(null != obj){
			ResourseInfo po = (ResourseInfo)super.getEntityById(ResourseInfo.class, obj.getId());
			po.setMarkDelete("Y");
			super.update(po);
		}	
	}

	@Override
	public void save(ResourseBizBean obj) {
		if(null != obj){
			ResourseInfo po = new ResourseInfo();
			PropertyUtils.copyProperties(po, obj);
			if(null != obj.getParentResourseBizBean()){
				ResourseInfo poP = new ResourseInfo();
				PropertyUtils.copyProperties(poP, obj.getParentResourseBizBean());
				po.setParentResourseInfo(poP);
			}
			super.save(po);
		}
	}



	@Override
	public Integer getIdByTitle(String title) {
		String sql = "SELECT msr.`ID` FROM `m_system_resource` msr where msr.`TITLE` = '"+title+"' and msr.mark_delete = 'N'";
		Integer listObject = super.getCountBySQL(sql);
		if(null != listObject && listObject > 0){
			return listObject;
		} else {
			return -1;
		}
	}



	@Override
	public List<ResourseInfo> getMenu(QueryPage queryPage) {
		String hql = "from ResourseInfo menu where menu.markDelete = 'N' order by orderBy asc";
		//String hql = "from ResourseInfo menu left join fetch menu.childrenList c where menu.markDelete = 'N' order by menu.orderBy asc";
		
		if (queryPage == null) {
			return this.findByHQL(hql, null);
		} else {
			return this.findByHQL(queryPage, hql,null);
		}
	}



	@Override
	public Integer getMaxId() {
		String hql ="select max(m.id) from ResourseInfo m";
		List<Object> results = this.findByHQL(hql,null);
		if (results != null && results.size() > 0) {
			Long maxId = null;
			if(null != results.get(0)){
				maxId = (Long)results.get(0);
			}
			return maxId.intValue();
		}
		return null;
	}
}
